using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Controls;
using System.Data;
using Lizk.WebScraper;
using System.IO;

namespace SharpMedia.Pages
{
    public class DVDSelectionPage : MediaPage
    {
        HUDImage box, icon, button;
        HUDLabel lblTitle, lblPress;
        HUDContainerSimple boxContainer;
        HUDCentering centering;
        HUDAlphaList list;
        HUDImage selection;
        TimedTrigger<string> trigger = new TimedTrigger<string>("", new TimeSpan(0, 0, 0, 0, 250));
        public DVDSelectionPage(HUDPage parent)
            : base(parent)
        {
            this.parent = parent;

            this.Anchor = Anchor.All;
            InitializeComponents();
        }

        public DVDSelectionPage(HUDPage parent, string drive)
            : base(parent)
        {
            this.parent = parent;

            this.Anchor = Anchor.All;
            InitializeComponents();

            for (int i = 0; i < list.Count; i++)
            {
                DriveInfo di = list[i].Tag as DriveInfo;
                if (di != null && di.RootDirectory.FullName.ToLower().StartsWith(drive.ToLower()))
                {
                    list.SelectedIndex = i;
                    UpdateSelection(true);
                    break;
                }
            }

        }

        
        private void InitializeComponents()
        {
            HUDImage background = new HUDImage("white.png");
            background.Size = Size;
            background.Color = ColorS.Black;
            background.ConstrainProportions = false;
            background.Alpha = 170;
            background.Anchor = Anchor.All;
            Add(background);


            boxContainer = new HUDContainerSimple();
            boxContainer.Size = new SizeS(500, 400);
            

            centering = new HUDCentering(this, boxContainer);
            centering.Anchor = Anchor.All;
            Add(centering);


            box = new HUDImage("box.png");
            box.ConstrainProportions = false;
            box.Size = boxContainer.Size;
            boxContainer.Add(box);

            icon = new HUDImage("dvd.png");
            icon.Size = new SizeS(64, 64);
            icon.Location = new PointS(20, 20);
            boxContainer.Add(icon);

            selection = new HUDImage("white.png");
            selection.Color = ColorS.Black;
            selection.Alpha = 127;
            selection.ConstrainProportions = false;
            boxContainer.Add(selection);

            list = new HUDAlphaList(new SizeS(460, 22), false);
            list.Bounds = new RectangleS(20, icon.Bounds.Bottom + 10, 460, 400 - icon.Bounds.Bottom - 20);
            boxContainer.Add(list);

            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                if (di.DriveType != DriveType.CDRom)
                    continue;

                HUDLabel lbl = new HUDLabel(di.Name, "tahoma20b");
                if (di.IsReady)
                    lbl.Text += " " + di.VolumeLabel;
                lbl.Color = di.IsReady ? ColorS.White : ColorS.Red;
                lbl.Size = list.ItemSize;
                lbl.CharacterOffset = new PointS(0, -4);
                lbl.Tag = di;
                list.Add(lbl);
            }
            
            
            UpdateSelection(false);

            lblTitle = new HUDLabel("Select DVD Drive", "tahoma30b");
            lblTitle.Size = new SizeS(boxContainer.Size.Width - icon.Bounds.Right - 10, 64);
            lblTitle.Location = new PointS(icon.Bounds.Right + 5, icon.Bounds.Top);
            lblTitle.Alignment = Alignment.VCenter | Alignment.Left;
            boxContainer.Add(lblTitle);

            button = new HUDImage("webbutton.png");
            button.Size = new SizeS(70, 70);
            button.Location = new PointS(boxContainer.Size.Width - 100,boxContainer.Size.Height - 100);
            //boxContainer.Add(button);
            
            lblPress = new HUDLabel("Press web/ok button to Save", "tahoma20b");
            lblPress.Size = new SizeS(boxContainer.Size.Width -110, 70);
            lblPress.Location = new PointS(0, button.Bounds.Top);
            lblPress.Alignment = Alignment.VCenter | Alignment.Right;
            //boxContainer.Add(lblPress);

            trigger.TimeOut += new EventHandler(trigger_TimeOut);
            trigger.Trigger();
        }

        void trigger_TimeOut(object sender, EventArgs e)
        {
            foreach (HUDLabel c in list)
            {
                c.Color = c.GetTag<DriveInfo>().IsReady ? ColorS.White : ColorS.Red;
                c.Text = c.GetTag<DriveInfo>().Name;
                if (c.GetTag<DriveInfo>().IsReady)
                    c.Text += " " + c.GetTag<DriveInfo>().VolumeLabel;

            }
            trigger.Trigger();
        }

        public override void Show()
        {
            Fade(255, 6f);
            trigger.Trigger();
            base.Show();
        }

        public override void Activate()
        {
            Fade(255, 6f);
            trigger.Trigger();
            base.Activate();
        }

        public override void Close()
        {
            Fade(0, 6f);
            trigger.Stop(false);
            base.Close();
        }
        public override void Update(TimeSpan ts)
        {
            trigger.Update();
            base.Update(ts);
        }

        public void UpdateSelection(bool animate)
        {
            list.MoveItems(animate);
            selection.Destination = list.Location + list.Selection.Location;
            selection.DestinationSize = list.Selection.Size;
            if (!animate)
            {
                selection.Location = selection.Destination;
                selection.Size = selection.DestinationSize;
            }

        }

        public override void Command(Lizk.SimpleHUD.HUDCommands command, object tag)
        {
            switch (command)
            {
                case Lizk.SimpleHUD.HUDCommands.Up:
                    list.SelectedIndex--;
                    UpdateSelection(true);
                    break;
                case Lizk.SimpleHUD.HUDCommands.Down:
                    list.SelectedIndex++;
                    UpdateSelection(true);
                    break;
                case Lizk.SimpleHUD.HUDCommands.Back:
                    SharpMedia.Back();
                    break;
                case Lizk.SimpleHUD.HUDCommands.Left:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Right:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PageUp:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PageDown:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Select:
                    if(list.Count == 0 || !list.SelectedControl.GetTag<DriveInfo>().IsReady)
                        return;
                    SharpMedia.Back();

                    VideoList vl = new VideoList();
                    vl.Add(new VideoSource(list.SelectedControl.GetTag<DriveInfo>().VolumeLabel, Path.Combine(list.SelectedControl.GetTag<DriveInfo>().RootDirectory.FullName, "video_ts/video_ts.ifo")));

                    PlayerPage dvp = new PlayerPage(this, vl);
                    SharpMedia.Navigate(dvp);

                    break;
                case Lizk.SimpleHUD.HUDCommands.Menu:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset1:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset2:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset3:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Play:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Pause:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PlayPause:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Stop:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Forward:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Rewind:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Next:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Previous:
                    break;
                case Lizk.SimpleHUD.HUDCommands.KeyPress:
                    break;
                default:
                    if (parent != null)
                        parent.Command(command, tag);
                    break;
            }
        }
    }
}
